import Common from '../common'
var Js = Common('productpage')
export default {
	name: 'productpage',
	mixins: [Js],
	components: {},
	data() {
		return {
      productData: {//存储的product的数据
      	role: 3,
      	title: '',
      	description: '',
      	price: '',
        f_price: '',
      	listorder: '', //排序
      	mainImg: [],
      	content: '',
      	bannerImg: [],
      	category_id: null,
      	shop_no: '',
      	description:'',
      	stock_type:1,
        stock:999
      },
      originProductData: {//存储的product的数据
      	role: 3,
      	title: '',
      	description: '',
      	price: '',
      	f_price: '',
      	listorder: '', //排序
      	mainImg: [],
      	content: '',
      	bannerImg: [],
      	category_id: null,
      	shop_no: '',
      	description:'',
      	stock_type:1,
        stock:999
      },
      originSkuData: {
				title: '',
				stock: 999,
				price: '',
				group_price:'',
				mainImg: [],
				one_level:'',
				two_level:'',
        stock_type:1
			},
      submitSku: {},
			searchItem: {},


			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			},

			type: 0, //0添加 1修改

      submitProduct:{
        stock:999
      },
			category_id:[],
			labelOptions: [],
			shopOptions: [],
			checkList: [],
			parentid: 0,
			skuLabelData: [], //sku类别
			skuTitle: '',
			titleId: 0,
			add: false,
			skuItemTit: '',
			skuData: [], //sku

			editsku: {
				title: '',
				stock: '',
				price: '',
				group_price:'',
				mainImg: [],
				one_level:'',
				two_level:''
			},
			edit: 0,
			mainData:{
        id:1,
      },
			productType:0,    //1.商城正常商品2.商城限时商品3.商城团购商品 4.民宿房间5.民宿商品 6.外卖商品
			behavior:0,
      product_id:''
		}

	},

	mounted() {
		const self = this;

		console.log('this.$route.params', this.$route.params)
		self.shop_no = this.$route.params.shop_no;
    if(this.$route.params.product_id){
      self.product_id = this.$route.params.product_id;
    };
		// self.productType = this.$route.params.types;
		// self.behavior = this.$route.params.behavior;
		// self.parentid = this.$route.params.parentid;
		self.init();

	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

    backRoute(){
      const self = this;
     var fullPath = self.$route.fullPath;
     var tabList = self.$store.state.tabs.tabs;
     var routerRes = self.$$getBackRoute(tabList,fullPath);
     self.$store.commit('REMOVE_TABS', {
       fullPath:fullPath,
     });
     if(routerRes.backRes){
       self.$router.push(routerRes.backRes);
     }else{
       self.$router.push(routerRes.newRes);
     };
    },

		/**
		 * 初始化
		 */
		init() {
			const self = this;
      //获取商品类别
			self.initLabelData();
			self.getShopData();
			if (self.product_id) {
				this.getProductData();
			};
      self.submitSku = self.$$cloneForm(self.originSkuData);
		},

    //获取商家
    async getShopData() {

    	const self = this;
    	const postData = {};
    	postData.token = self.$store.getters.getToken;
    	postData.searchItem = {
    		shop_no: self.shop_no,
    	};
    	var res = await self.$$api_shopGet({
    		data: postData
    	});
    	self.shopData = res.info.data[0];
    },



    //获取类别
    async initLabelData(isNew,shop_no) {

    	const self = this;
    	const postData = {};
    	postData.token = self.$store.getters.getToken;
    	postData.searchItem = {
    		type: 4,
    		shop_no:self.shop_no,
    	};
    	var res = await self.$$api_labelGet({
    		data: postData
    	});
    	self.labelOptions = res.info.data;

    },




		//添加商品
		async productSubmit() {
			const self = this;

      var res = self.checkSubmitProduct();
      if(!res){
        return;
      };

			var postData = {};
			postData.token = self.$store.getters.getToken;
			postData.data = self.$$cloneForm(self.submitProduct);

      if(!self.product_id){

        postData.data.user_no = self.shopData['user_no'];
        postData.data.shop_no = self.shopData['shop_no'];
        postData.data.role = self.shopData['role'];
        postData.data.thirdapp_id = self.shopData['thirdapp_id'];

        var res = await self.$$api_productAdd({
        	data: postData
        });
        if (res.solely_code == 100000) {
        	this.$message({
        		message: '添加成功',
        		type: 'success'
        	});
        	self.product_id = res.info.id;
        	self.getProductData();
        }
      }else{
        postData.searchItem = {};
        postData.searchItem.id = self.product_id;
        delete(postData.data.id);
        delete(postData.data.label);
        delete(postData.data.user_no);
        delete(postData.data.role);
        delete(postData.data.thirdapp_id);

        postData.data.category_id = postData.data.category_id.join(',');
        var res = await self.$$api_productUpdate({
        	data: postData
        });
        if (res.solely_code == 100000) {
        	this.$message({
        		message: '修改成功',
        		type: 'success'
        	});
        };
      };

		},

    checkSubmitProduct(){

      const self = this;
      console.log('checkSubmitProduct',self.submitProduct)
      if(self.product_id){
        if (self.submitProduct&&self.submitProduct.category_id=='') {
        	this.$message({
        		message: '请选择商品类别',
        		type: 'warning'
        	});
        	return false;
        };
      }else{
        if (!self.submitProduct.title || !self.submitProduct.price) {
        	this.$message({
        		message: '请完善商品名称和售价',
        		type: 'warning'
        	});
        	return false;
        };

        if (!self.submitProduct.category_id) {
        	this.$message({
        		message: '请选择商品类别',
        		type: 'warning'
        	});
        	return false;
        };
      };

      return true;
    },

    async getProductData() {

    	const self = this;
    	const postData = {};
    	postData.token = self.$store.getters.getToken;
    	postData.searchItem = {
        id:self.product_id
      };
    	var res = await self.$$api_productGet({
    		data: postData
    	});
    	self.productData = res.info.data[0];
      self.productData.category_id = self.productData.category_id.split(',');
      self.submitProduct = self.$$cloneForm(self.productData);
    	self.getSkuData();
    },

    async getSkuData() {
    	const self = this;
    	const postData = {};
    	postData.token = self.$store.getters.getToken;
    	postData.searchItem = {
    		product_id: self.product_id
    	}
    	postData.order = {
    		create_time: 'asc'
    	}
    	var res = await self.$$api_skuGet({
    		data: postData
    	});
    	self.skuData = res.info.data;
      self.submitSku = self.$$cloneForm(self.originSkuData);
    },



		clearPro() {
			const self = this;
      self.productData = self.$$cloneForm(self.originProductData);
      self.submitProduct = {};
			self.product_id = '';
			self.mainData = {};
			self.skuLabelData = [];
			self.skuData = [];
      self.submitSku = self.$$cloneForm(self.originSkuData);
		},

		async skuSubmit() {
			const self = this;

      if(!self.product_id){
        self.$message({
        	message: '请先添加product',
        	type: 'warning'
        });
        return;
      };
      var res = self.checkSubmitSku();
      if(!res){
        return;
      };
      const postData = {
      	token: self.$store.getters.getToken,
      	data: self.$$cloneForm(self.submitSku),
      };
      if(self.submitSku.id){
        postData.searchItem = {
          id:self.submitSku.id
        };
        delete(postData.data.id);
        delete(postData.data.product);
        delete(postData.data.user_no);
        delete(postData.data.role);
        delete(postData.data.thirdapp_id);
        var res = await self.$$api_skuUpdate({
        	data: postData
        });
      }else{
        postData.data.product_id = self.productData['id'];
        postData.data.product_no = self.productData['product_no'];
        postData.data.user_no = self.shopData['user_no'];
        postData.data.shop_no = self.shopData['shop_no'];
        postData.data.role = self.shopData['role'];
        postData.data.thirdapp_id = self.shopData['thirdapp_id'];
        var res = await self.$$api_skuAdd({
        	data: postData
        });
      };
      console.log('res',res)
      if (res.solely_code == 100000) {
      	self.$message({
      		message: self.submitSku.id?'修改成功':'添加成功',
      		type: 'success'
      	});
        self.getSkuData();
      }else{
        self.$message({
        	message: '操作失败',
        	type: 'warn'
        });
      };

		},



    checkSubmitSku(){

      const self = this;

      if(!self.submitSku.id){
        if (!self.submitSku.title || !self.submitSku.price || !self.submitSku.stock || self.submitSku.mainImg
        	.length <= 0) {
        	self.$message({
        		message: '请完善商品SKU信息',
        		type: 'warning'
        	});
        	return false;
        }
      };
      return true;
    },


    async deleteSku(item) {
    	const self = this;
    	self.$confirm('此操作将删除该商品sku, 是否继续?', '提示', {
    		confirmButtonText: '确定',
    		cancelButtonText: '取消',
    		type: 'warning'
    	}).then(async () => {
    		var data = {
    			status: -1
    		};
    		var res = await self.$$api_skuUpdate({
          searchItem:{
            id:item.id
          },
    			data: {status:-1}
    		});
        if (res.solely_code == 100000) {
        	self.$message({
        		message: '删除成功',
        		type: 'success'
        	});
          self.getSkuData();
        }else{
          self.$message({
          	message: '删除失败',
          	type: 'warn'
          });
        };
    	}).catch(() => {
    		self.$message({
    			type: 'info',
    			message: '取消删除'
    		});
    	});
    },

    //分水岭

		chooseSku(item) {
			const self = this;
			self.edit = item.id;
			self.editsku = {
				title: item.title,
				stock: item.stock,
				price: item.price,
				group_price:item.group_price,
				mainImg: item.mainImg,
				one_level: item.one_level,
				two_level: item.two_level,
			}
		},

		editSku(item) {
			const self = this;
			self.$confirm('此操作将修改该商品sku信息, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				self.updateSku(item, self.editsku, 1);
			}).catch(() => {
				self.$message({
					type: 'info',
					message: '取消修改'
				});
			});
		},



		async initSkuLabelData(isNew) {
			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				type: ['in', [5, 6]],
				relation_id: self.mainData.id
			}
			postData.order = {
				create_time: 'asc'
			}
			var res = await self.$$api_labelGet({
				data: postData
			});
			self.skuLabelData = res.info.data;
		},

		async addSkuLabel(type, item) {
			const self = this;
			const postData = {};
			if ((!self.skuTitle && type == 5) || (!self.skuItemTit && type == 6)) {
				self.$message({
					message: '请填写名称',
					type: 'warning'
				});
				return;
			}
			postData.token = self.$store.getters.getToken;
			postData.data = {
				type: type,
				relation_id: self.mainData.id
			}
			if (type == 5) {
				postData.data.title = self.skuTitle
			} else {
				postData.data.title = self.skuItemTit;
				postData.data.parentid = item.id;
			}
			var res = await self.$$api_labelAdd({
				data: postData
			});
			self.skuTitle = '';
			self.skuItemTit = '';
			self.itemId = 0;
			self.add = false;
			self.initSkuData();
		},

		async updateSkuLabel(item, type) {
			const self = this;
			const postData = {};
			if (!item.title) {
				self.$message({
					message: '名称不可为空',
					type: 'warning'
				});
				return;
			}
			postData.token = self.$store.getters.getToken;
			postData.data = {
				title: item.title
			}
			postData.searchItem = {
				id: item.id
			}
			var res = await self.$$api_labelUpdate({
				data: postData
			});
			self.titleId = 0;
			self.initSkuData();
		},

		deleteCon(id, type) {
			const self = this;
			if (type) {
				var title = '此操作将删除该类别及子集类别, 是否继续?';
			} else {
				var title = '此操作将删除该类别, 是否继续?'
			}
			self.$confirm(title, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				self.deleteSkuLabel(id, type);
			}).catch(() => {
				self.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		async deleteSkuLabel(id, type) {
			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.data = {
				status: -1
			}

			if (type) {
				postData.searchItem = {
					parentid: id
				}
				postData.saveAfter = [{
					tableName: 'Label',
					FuncName: 'update',
					data: {
						status: -1
					},
					searchItem: {
						id: id
					}
				}]
			} else {
				postData.searchItem = {
					id: id
				}
			}
			var res = await self.$$api_labelUpdate({
				data: postData
			});
			self.$message({
				type: 'success',
				message: '删除成功!'
			});
			self.titleId = 0;
			self.initSkuData();
		},
	},


}
